<template>
  <div class="goodslist" ref="listHeight" @scroll.passive="onscroll">
    <template v-for="(item, index) in goodsinfo">
      <div class="wrap" :key="index" ref="ulHeight">
        <p class="title" ref="pdom">{{ item.title }}</p>
        <ul :key="index">
          <template v-for="(item, index) in item.goods">
            <li :key="index">
              <img v-lazy="item.imgUrl" alt="" />
              <p>{{ item.name }}</p>
            </li>
          </template>
        </ul>
      </div>
    </template>
  </div>
</template>

<script>
export default {
  props: {
    goodsinfo: Array, //商品信息
  },
  data() {
    return {
      inputvalue: "1", //搜索框的值,
      aaa: "下装",
    };
  },
  computed: {
    // 让第x个元素滚动
    fn() {
      return this.$store.state.sidebarindex;
    },
  },
  watch: {
    fn(val1, val2) {
      this.$nextTick(() => {
        this.$refs["pdom"].forEach((element, index) => {
          if (element.innerHTML == this.$store.state.keyword) {
            this.$refs["listHeight"].scrollTop = this.$refs["ulHeight"][
              val1
            ].offsetTop;
          }
        });
      });
    },
  },
  methods: {
    onscroll(e) {
      if (this.$store.state.clickflag) {
        let arr = [];
        let str = "";
        this.$refs["ulHeight"].forEach((element) => {
          if (
            e.target.scrollTop >= element.offsetTop &&
            !arr.includes(element.firstChild.innerHTML)
          ) {
            arr.push(element.firstChild.innerHTML);
            str = element.firstChild.innerHTML;
          }
          this.$store.commit("getKeyWord", str);
        });
      }else{
         this.$store.state.clickflag = true;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.goodslist {
  position: relative;
  height: 1200px;
  overflow: scroll;
  &::-webkit-scrollbar {
    display: none;
  }
  .wrap {
    background-color: #ffffff;
    border-radius: 30px;
    margin-bottom: 20px;
    position: relative;
  }
  .title {
    text-align: left;
    font-size: 28px;
    color: #333333;
    padding-top: 19px;
    padding-left: 23px;
  }
  ul {
    width: 513px;
    display: flex;
    flex-wrap: wrap;
    li {
      flex: 1;
      width: 163px;
      margin: 10px 0;
      font-size: 0;
      img {
        width: 163px;
        height: 186px;
      }
      p {
        color: #333333;
        font-size: 22px;
        margin-top: 15px;
        margin-bottom: 5px;
      }
    }
  }
}
</style>